<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\exception\ValidateException;

use app\common\model\{Label, Audience, UserLabel, Tutor, Matchmaker, Consultant, Service};
use app\common\validate\{AudienceValidate, TutorValidate, MatchmakerValidate, ConsultantValidate, ServiceValidate};

class ApplyController extends BaseController
{
    /** 标签列表 */
    public function label_list()
    {
        $data = input('');

        try{
            validate(AudienceValidate::class)->scene('type')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $labelModel = new Label();

        $field = 'id, name';
        $where[] = ['type', '=', $data['type']];

        /** 查询标签列表 */
        $list = $labelModel->getList($where, 0, 0, 0, $field);

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        $this->success_back('ok', $list['data']);
    }

    /** 申请听客 */
    public function audience_add()
    {
        $data = input('');

        try{
            validate(AudienceValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $audienceModel = new Audience();
        $userLabelModel = new UserLabel();

        /** 查询是否提交过申请 */
        $where[] = ['user_id', '=', $data['user_id']];
        $where[] = ['status', '<>', 3];
        $audience_info = $audienceModel->getFind($where, 'id');

        if($audience_info['code'] == 'ok'){
            $this->error_back('已提交过申请，请耐心等待审核。');
        }

        /** 新增申请 */
        $field = ['user_id', 'home_status', 'address', 'resume', 'card_img', 'card_img_back', 'edu_img', 'qua_img'];
        $flag = $audienceModel->add($data, $field);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 标签ID */
        $label_ids = explode(',', $data['label_id']);

        $label_arr = [];
        foreach($label_ids as $v){
            $label_info = [
                'user_id'  => $data['user_id'],
                'obj_id'   => $flag['data'],
                'obj_type' => 1,
                'label_id' => $v,
            ];

            $label_arr[] = $label_info;
        }

        /** 添加听客标签 */
        $label_flag = $userLabelModel->addAll($label_arr);

        if($label_flag['code'] != 'ok'){
            /** 添加失败，删除已新增的申请 */
            $audienceModel->del(['id' => $flag['data']]);
            $this->error_back('申请失败，请稍后重试。');
        }

        $this->success_back('申请已提交，请耐心等待审核');
    }

    /** 申请婚恋导师 */
    public function tutor_add()
    {
        $data = input('');

        try{
            validate(TutorValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $tutorModel = new Tutor();
        $userLabelModel = new UserLabel();

        /** 查询是否提交过申请 */
        $where[] = ['user_id', '=', $data['user_id']];
        $where[] = ['status', '<>', 3];
        $tutor_info = $tutorModel->getFind($where, 'id');

        if($tutor_info['code'] == 'ok'){
            $this->error_back('已提交过申请，请耐心等待审核。');
        }

        /** 新增申请 */
        $field = ['user_id', 'address', 'honor', 'expert', 'edu_img', 'qua_img'];
        $flag = $tutorModel->add($data, $field);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 标签ID */
        $label_ids = explode(',', $data['label_id']);

        $label_arr = [];
        foreach($label_ids as $v){
            $label_info = [
                'user_id'  => $data['user_id'],
                'obj_id'   => $flag['data'],
                'obj_type' => 2,
                'label_id' => $v,
            ];

            $label_arr[] = $label_info;
        }

        /** 添加标签 */
        $label_flag = $userLabelModel->addAll($label_arr);

        if($label_flag['code'] != 'ok'){
            /** 添加失败，删除已新增的申请 */
            $tutorModel->del(['id' => $flag['data']]);
            $this->error_back('申请失败，请稍后重试。');
        }

        $this->success_back('申请已提交，请耐心等待审核');
    }

    /** 申请红娘 */
    public function matchmaker_add()
    {
        $data = input('');

        try{
            validate(MatchmakerValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $matchmakerModel = new Matchmaker();

        /** 查询是否提交过申请 */
        $where[] = ['user_id', '=', $data['user_id']];
        $where[] = ['status', '<>', 3];
        $tutor_info = $matchmakerModel->getFind($where, 'id');

        if($tutor_info['code'] == 'ok'){
            $this->error_back('已提交过申请，请耐心等待审核。');
        }

        /** 新增申请 */
        $field = ['user_id', 'edu_img', 'qua_img'];
        $flag = $matchmakerModel->add($data, $field);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('申请已提交，请耐心等待审核');
    }

    /** 申请咨询师 */
    public function consultant_add()
    {
        $data = input('');

        try{
            validate(ConsultantValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $ConsultantModel = new Consultant();
        $userLabelModel = new UserLabel();

        /** 查询是否提交过申请 */
        $where[] = ['user_id', '=', $data['user_id']];
        $where[] = ['status', '<>', 3];
        $tutor_info = $ConsultantModel->getFind($where, 'id');

        if($tutor_info['code'] == 'ok'){
            $this->error_back('已提交过申请，请耐心等待审核。');
        }

        /** 新增申请 */
        $field = ['user_id', 'address', 'honor', 'expert', 'edu_img', 'qua_img'];
        $flag = $ConsultantModel->add($data, $field);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 标签ID */
        $label_ids = explode(',', $data['label_id']);

        $label_arr = [];
        foreach($label_ids as $v){
            $label_info = [
                'user_id'  => $data['user_id'],
                'obj_id'   => $flag['data'],
                'obj_type' => 3,
                'label_id' => $v,
            ];

            $label_arr[] = $label_info;
        }

        /** 添加标签 */
        $label_flag = $userLabelModel->addAll($label_arr);

        if($label_flag['code'] != 'ok'){
            /** 添加失败，删除已新增的申请 */
            $ConsultantModel->del(['id' => $flag['data']]);
            $this->error_back('申请失败，请稍后重试。');
        }

        $this->success_back('申请已提交，请耐心等待审核');
    }

    /** 申请服务站 */
    public function service_add()
    {
        $data = input('');

        try{
            validate(ServiceValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $serviceModel = new Service();

        /** 查询是否提交过申请 */
        $where[] = ['user_id', '=', $data['user_id']];
        $where[] = ['status', '<>', 3];
        $tutor_info = $serviceModel->getFind($where, 'id');

        if($tutor_info['code'] == 'ok'){
            $this->error_back('已提交过申请，请耐心等待审核。');
        }

        /** 新增申请 */
        $field = ['user_id', 'name', 'address', 'site_layout', 'staffing', 'edu_img', 'qua_img', 'site_head_img', 'interior_img'];
        $flag = $serviceModel->add($data, $field);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('申请已提交，请耐心等待审核');
    }
}